// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue';
// 引入element UI
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import App from './App';
// 引入路由
import router from './router';

// 引入echarts
import echarts from 'echarts'

Vue.prototype.$echarts = echarts

import axios from 'axios';
import Request from "./api/request";

Vue.prototype.$axios = axios;

Vue.config.productionTip = false;

// 使用element UI
Vue.use(ElementUI);

// 路由拦截器
function count() {
    Request.get("/online/count")
}

router.beforeEach((to, from, next) => {
    if (to.matched.length != 0) {
        if (to.meta.requireAuth) { // 判断该路由是否需要登录权限
            if (Boolean(localStorage.getItem("token"))) { // 通过vuex state获取当前的user是否存在
                next();
                count()
            } else {
                next({
                    path: '/login',
                    query: {redirect: to.fullPath} // 将跳转的路由path作为参数，登录成功后跳转到该路由
                })
            }
        } else {
            if (Boolean(localStorage.getItem("token"))) { // 判断是否登录
                if (to.path != "/blogin" && to.path != "/u/login") { //判断是否要跳到登录界面
                    next();
                } else {
                    /**
                     * 防刷新，如果登录，修改路由跳转到登录页面，修改路由为登录后的首页
                     */
                    next({
                        path: '/u/dashboard'
                    })
                    count()
                }
            } else {
                next();
                count()
            }
        }
    } else {
        next({
            path: '/',
            query: {redirect: to.fullPath} // 将跳转的路由path作为参数，登录成功后跳转到该路由
        })
        count()
    }
})
new Vue({
    router,
    render: h => h(App)
}).$mount("#app")